package com.ranjiaqi.homework.managersystem;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @ClassName Methods
 * @Description TODO
 * @Author hp
 * @Date 2021/6/25 18:17
 * @Version 1.0
 **/
public class Methods {
    private List<Custom> customList = new ArrayList<Custom>();
    private Scanner sc = new Scanner(System.in);


    public void menu() {
        while (true) {
            System.out.println("欢迎使用客户管理系统");
            System.out.println("1.添加用户");
            System.out.println("2.修改用户");
            System.out.println("3.删除用户");
            System.out.println("4.查询用户");
            System.out.println("5.退出程序");
            System.out.print("请根据数字，选择您的操作:");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    addCuntoms();
                    break;
                case 2:
                    changeCustoms();
                    break;
                case 3:
                    deleteCustoms();
                    break;
                case 4:
                    findCustoms();
                    break;
                case 5:
                    System.exit(0);
                    break;

                default:

                    System.out.println("输入有误");
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
        }
    }

    private void findCustoms() {
        if (customList.isEmpty()){
            init();
        }
        System.out.println(customList);
    }

    private void deleteCustoms() {
        if (customList.isEmpty()){
            init();
        }
        System.out.println("请输入你要删除的用户名称：");
        String name = sc.next();
        boolean isEmpty = true;
        for (int i = 0; i < customList.size(); i++) {
            if (customList.get(i).getName().equals(name)){
                System.out.println(i+" "+customList.get(i).toString());
                isEmpty = false;
            }
        }
        if (!isEmpty){
            System.out.println("请输入你要删除的角色序号");
            int num = sc.nextInt();
            customList.remove(num);
        }else {
            System.out.println("此用户不存在删除失败");
        }
    }

    private void init() {
        customList.add(new Custom("客户名称","性别",18));
    }

    private void changeCustoms() {

        System.out.println("请输入你要修改的用户名称：");
        String name = sc.next();
        boolean isEmpty = true;
        for (int i = 0; i < customList.size(); i++) {
            if (customList.get(i).getName().equals(name)){
                System.out.println(i+" "+customList.get(i).toString());
                isEmpty = false;
            }
        }
        if (!isEmpty){
            System.out.println("请输入你要修改的角色序号");
            Integer num = sc.nextInt();
            customList.set(num,creatNewCustom());
        }else {
            System.out.println("此用户不存在修改失败");
        }
    }

    private Custom creatNewCustom() {
        Custom custom = new Custom();
        System.out.println("请输入用户的姓名:");
        custom.setName(sc.next());
        System.out.println("请输入用户的年龄");
        custom.setAge(sc.nextInt());
        System.out.println("请输入用户的性别");
        custom.setGender(sc.next());
        return custom;
    }

    private void addCuntoms() {
        Custom custom = creatNewCustom();
        boolean repetition = false;
        for (Custom custom1 :customList){
            repetition = custom1.getName().equals(custom.getName())&&custom1.getAge().equals(custom.getAge())
                    &&custom1.getGender().equals(custom.getGender());
        }
        if (repetition){
            System.out.println("该用户已经存在");
        }else {
            customList.add(custom);
        }
    }


}
